﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EqualsShould.cs" company="SaltFx">
//   Copyright (c) SaltFx - All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace SaltFx.Tests.PersistableObject
{
	using System;
	using System.Collections.Generic;
	using System.Diagnostics;

	using NUnit.Framework;

	/// <summary>
	/// The PersistableObject GetHashCode method override tests.
	/// </summary>
	public class GetHashCodeShould
	{
		#region Public Methods

		/// <summary>
		/// Ensures a keyed object's surrogate id is used to hash when both are available.
		/// </summary>
		[Test]
		public void ReturnSurrogateIdHashGivenObjectHasKeyAndSurrogateId()
		{
			var sid = Guid.NewGuid();

			var obj = new KeyedTestPersistableObject("FOO", "PEANUT") { Sid = sid };

			Assert.IsTrue(obj.GetHashCode().Equals(obj.Sid.GetHashCode()));
		}

		/// <summary>
		/// Ensures that zero is returned if there is no key or surrogate id.
		/// </summary>
		[Test]
		public void ReturnZeroGivenObjectHasNoKeyOrSurrogateId()
		{
			var obj = new KeyedTestPersistableObject(null, "PEANUT");

			Debug.WriteLine(obj.GetHashCode());

			var rawObject = obj as object;

			Debug.WriteLine(rawObject.GetHashCode());

			Assert.IsTrue(obj.GetHashCode().Equals(rawObject.GetHashCode()));
		}

		/// <summary>
		/// Ensures a keyed object's surrogate id is used to hash when both are available.
		/// </summary>
		[Test]
		public void EnablesObjectsToBeReferencedInAListGivenHasKeyAndNoSurrogateId()
		{
			var objA = new KeyedTestPersistableObject("FOO", "PEANUT");
			var objB = new KeyedTestPersistableObject("FOO", "PEANUT");

			var testList = new List<KeyedTestPersistableObject> { objA };

			Assert.IsTrue(testList.Contains(objB));
		}

		/// <summary>
		/// Ensures a keyed object's surrogate id is used to hash when both are available.
		/// </summary>
		[Test]
		public void EnablesObjectsToBeReferencedForRemovalInAListGivenHasKeyAndNoSurrogateId()
		{
			var objA = new KeyedTestPersistableObject("FOO", "PEANUT");
			var objB = new KeyedTestPersistableObject("FOO", "PEANUT");

			var testList = new List<KeyedTestPersistableObject> { objA };

			testList.Remove(objB);

			Assert.IsTrue(testList.Count.Equals(0));
		}

		#endregion
	}
}